﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Linq;
using DevExpress.Utils;

namespace DevEx
{
    public partial class ucDVT : DevExpress.XtraEditors.XtraUserControl
    {
        public ucDVT()
        {
            InitializeComponent();
        }

        private void ucCCL_Load(object sender, EventArgs e)
        {
            KhoiTaodtgDVT("");
            KhoiTaoGiaoDien(true);
        }

        public void KhoiTaodtgDVT(String Sname)
        {
            //CDVT kho = new CDVT();
            //dtgDVT.DataSource = kho.SelectAllRecord();

            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblDVTs select new { p.ID_DVT ,p.DVT };
            dtgDVT.DataSource = q;

            gridView1.Columns.ColumnByFieldName("ID_DVT").Visible = false;

            //Columns 
            var cl_DVT = gridView1.Columns.ColumnByFieldName("DVT");            
            cl_DVT.Caption = "Đơn vị tính";
            cl_DVT.OptionsColumn.AllowEdit = false;
            cl_DVT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_DVT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;


            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView1.GetRowCellValue(i, "DVT").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }         

            gridView1.FocusedRowHandle = focusrow;
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtDVT.EditValue = gridView1.GetFocusedRowCellValue("DVT");
        }


        Boolean fThem = false;
        Boolean fSua = false;
        CDVT m_kho = new CDVT();

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtDVT.Focus();
            fThem = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtDVT.Focus();
            fSua = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //Kiem tra ton tai
            int ID_DVT = 0;
            ID_DVT = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_DVT"));

            QLVTDataContext dc = new QLVTDataContext();

            int c_DVT;
            c_DVT = (from p in dc.tblVatTus
                     where p.ID_DVT == ID_DVT
                     select p).Count();
            if (c_DVT > 1)
                MessageBox.Show("Đơn vị tính này đang được sử dụng, không xóa được.");

            String DVT = Convert.ToString(gridView1.GetFocusedRowCellValue("DVT"));
            if (MessageBox.Show("Bạn muốn xóa ĐVT: " + DVT, "Chú ý", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                
                m_kho.Delete(ID_DVT);
                KhoiTaodtgDVT("");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (fThem)
            {
                tblDVT dvt = new tblDVT();
                 dvt.DVT= txtDVT.Text;
                m_kho.Insert(dvt);
            }

            if (fSua)
            {
                tblDVT dvt = new tblDVT();
                dvt.ID_DVT = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_DVT"));
                dvt.DVT = txtDVT.Text;
                m_kho.Update(dvt);
            }

            KhoiTaoGiaoDien(true);
            KhoiTaodtgDVT(txtDVT.Text);
            fThem = false;
            fSua = false;

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(true);
            fThem = false;
            fSua = false;
        }

        public void KhoiTaoGiaoDien(Boolean f)
        {
            btnThem.Enabled = f;
            btnSua.Enabled = f;
            btnXoa.Enabled = f;
            btnGhi.Enabled = !f;
            btnHuy.Enabled = !f;
            txtDVT.Properties.ReadOnly = f;
        }
        
    }
}
